//
//  HXNet.m
//  HXYJ
//
//  Created by Sven on 14-12-14.
//  Copyright (c) 2014年 HX. All rights reserved.
//

#import "ICNet.h"
#import "AFNetworking.h"
#import "OpenUDID.h"
//#import "NSData+MD5.h"
#import "ICCompletePofileInfo.h"
//#import "ICProjectSimpleItem.h"
#import "EDJHttpOperation.h"
#import "AFNetworkActivityIndicatorManager.h"
#import "EDJObjectData.h"
#import "EFUserLocationItem.h"

@interface ICNet()
@property(nonatomic, strong)NSURL *baseUrl;
@end

static ICNet *_instance = nil;
@implementation ICNet
//+ (ICNet *)shareInstance {
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        NSURL *url = [NSURL URLWithString:kBaseUrl];
//        _instance = [[ICNet alloc] initWithBaseURL:url];
//    });
//    return _instance;
//}

- (id)initWithBaseURL:(NSURL *)url {
    if ((self = [super initWithBaseURL:url])) {
        self.baseUrl = url;
        
        [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    }
    return self;
}

//+ (void)destroy{
//    _instance = nil;
//}

- (void)updateHeadImage:(NSData *)imageData  onComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:APP_CONTEXT.accountManager.accountInfo.token forKey:@"token"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    NSString *urlStr = [NSString stringWithFormat:@"%@user/upload/image",self.baseURL.absoluteString];
    [manager POST:urlStr parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg",str];
        [formData appendPartWithFileData:imageData name:@"headImg" fileName:fileName mimeType:@"image/jpg"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DLog(@"req = %@",task);
        DLog(@"Json = %@",responseObject);
        NSInteger status = [responseObject[@"status"] integerValue];
        NSString *errMsg = responseObject[@"msg"];
        block(status,responseObject,errMsg);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"req = %@",task);
        DLog(@"error = %@",error);
        NSInteger code = 0;
        if (error.code >= NSURLErrorTimedOut || error.code <= NSURLErrorNotConnectedToInternet) {
            code = NSURLErrorNotConnectedToInternet;
        }
        block(code,error.userInfo,@"网络请求失败");

    }];
}


- (void)updateImagesWithMethod:(NSString *)method
                          path:(NSString *)path
                    parameters:(NSDictionary *)parameters
               ImagesDataArray:(NSArray *)imageArray
                     NameArray:(NSArray *)nameArray
                    onComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:parameters];
    [dic setValue:APP_CONTEXT.accountManager.accountInfo.token forKey:@"token"];
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",self.baseURL.absoluteString,path];
    [manager POST:urlStr parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        
        for(NSInteger i=0; i<imageArray.count; i++){
            NSString *filename = [NSString stringWithFormat:@"%@.jpg",[nameArray objectAtIndex:i]];
            [formData appendPartWithFileData:[imageArray objectAtIndex:i] name:@"file" fileName:filename mimeType:@"image/jpg"];
        }
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DLog(@"req = %@",task);
        DLog(@"Json = %@",responseObject);
        NSInteger status = [responseObject[@"status"] integerValue];
        NSString *errMsg = responseObject[@"msg"];
        block(status,responseObject,errMsg);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        DLog(@"req = %@",task);
        DLog(@"error = %@",error);
        NSInteger code = 0;
        if (error.code >= NSURLErrorTimedOut || error.code <= NSURLErrorNotConnectedToInternet) {
            code = NSURLErrorNotConnectedToInternet;
        }
        block(code,error.userInfo,@"网络请求失败");
        
    }];

//    
//    NSURLRequest *request = [self multipartFormRequestWithMethod:method path:path parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
//    {
//        for(NSInteger i=0; i<imageArray.count; i++){
//            NSString *filename = [NSString stringWithFormat:@"%@.jpg",[nameArray objectAtIndex:i]];
//            [formData appendPartWithFileData:[imageArray objectAtIndex:i] name:@"file" fileName:filename mimeType:@"image/jpg"];
//        }
//    }];
//    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
//    AFJSONRequestOperation *jsonRequest = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//        DLog(@"req = %@",request);
//        DLog(@"Json = %@",JSON);
//        NSInteger status = [JSON[@"status"] integerValue];
//        NSString *errMsg = JSON[@"msg"];
//        block(status,JSON,errMsg);
//    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
//        DLog(@"req = %@",request);
//        DLog(@"error = %@",error);
//        NSInteger code = 0;
//        if (error.code >= NSURLErrorTimedOut || error.code <= NSURLErrorNotConnectedToInternet) {
//            code = NSURLErrorNotConnectedToInternet;
//        }
//        block(code,JSON,@"网络请求失败");
//    }];
//    [jsonRequest start];
}


- (void)baseReqeustWithMethod:(NSString *)method
                         path:(NSString *)path
                   parameters:(NSDictionary *)parameters
                   onComplete:(RSRequestComplete)block{
    NSMutableDictionary *mutPararmeters = [NSMutableDictionary dictionaryWithDictionary:parameters];
    if (APP_CONTEXT.accountManager.accountInfo.token.length > 0) {
        [mutPararmeters setValue:APP_CONTEXT.accountManager.accountInfo.token forKey:@"token"];
    }
    
    NSMutableURLRequest *request =[self requestWithMethod:method path:path parameters:mutPararmeters];
    
//    NSURL *url = request.URL;
//    NSString *string = [NSString string];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    if([method isEqual:@"POST"]){
        [manager POST:request.URL.absoluteString parameters:mutPararmeters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            DLog(@"req = %@",task);
            DLog(@"Json = %@",responseObject);
            NSInteger code = [responseObject[@"rCode"] integerValue];
            NSString *errMsg = responseObject[@"rMessage"];
            block(code,responseObject,errMsg);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            DLog(@"req = %@",task);
            DLog(@"error = %@",error);
            NSInteger code = 0;
            if (error.code >= NSURLErrorTimedOut || error.code <= NSURLErrorNotConnectedToInternet) {
                code = NSURLErrorNotConnectedToInternet;
            }
            block(code,error.userInfo,@"网络请求失败");
        }];
    }else{
        [manager GET:request.URL.absoluteString parameters:mutPararmeters progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            DLog(@"req = %@",task);
            DLog(@"Json = %@",responseObject);
            NSInteger code = [responseObject[@"rCode"] integerValue];
            NSString *errMsg = responseObject[@"rMessage"];
            block(code,responseObject,errMsg);
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            DLog(@"req = %@",task);
            DLog(@"error = %@",error);
            NSInteger code = 0;
            if (error.code >= NSURLErrorTimedOut || error.code <= NSURLErrorNotConnectedToInternet) {
                code = NSURLErrorNotConnectedToInternet;
            }
            block(code,error.userInfo,@"网络请求失败");
        }];
    }

    
//    [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
//    AFJSONRequestOperation *jsonRequest = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
//        DLog(@"req = %@",request);
//        DLog(@"Json = %@",JSON);
//        NSInteger status = [JSON[@"status"] integerValue];
//        NSString *errMsg = JSON[@"msg"];
//        block(status,JSON,errMsg);
//    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
//        
//        DLog(@"req = %@",request);
//        DLog(@"error = %@",error);
//        NSInteger code = 0;
//        if (error.code >= NSURLErrorTimedOut || error.code <= NSURLErrorNotConnectedToInternet) {
//            code = NSURLErrorNotConnectedToInternet;
//        }
//        block(code,JSON,@"网络请求失败");
//    }];
//    [jsonRequest start];
}

- (void)getUpdateInfo:(NSString *)currentVersion OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:currentVersion forKey:@"version"];
    [dic setValue:@"ios" forKey:@"osType"];
    [self baseReqeustWithMethod:@"GET" path:@"app/upgrade" parameters:dic onComplete:block];
}

- (void)loginWithPhoneNum:(NSString *)phoneNum password:(NSString *)password OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = @{@"phone":phoneNum.length > 0 ? phoneNum:@"",
                          @"password":password.length > 0 ? password:@"",
                          };
    [self baseReqeustWithMethod:@"POST" path:@"user/login" parameters:dic onComplete:block];
}


- (void)logOutWithToken:(NSString *)token clientId:(NSString *)clientId onComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:token forKey:@"token"];
    [dic setValue:clientId forKey:@"clientId"];
    [self baseReqeustWithMethod:@"POST" path:@"user/logout" parameters:dic onComplete:block];
}

- (void)fineProjectComplete:(RSRequestComplete)block{
    [self baseReqeustWithMethod:@"POST" path:@"project/index" parameters:nil onComplete:block];
}


- (void)registerUserPhone:(NSString *)phone password:(NSString *)password vcode:(NSString *)vcode vcodeKey:(NSString *)vcodeKey onComplete:(RSRequestComplete)block{
    NSDictionary *dic = @{
                          @"phone":phone.length > 0 ? phone:@"",
                          @"password":password.length > 0 ? password:@"",
                          @"vcode":vcode.length > 0 ? vcode:@"",
                          @"vcodeKey":vcodeKey > 0? vcodeKey:@""
                          };
    [self baseReqeustWithMethod:@"POST" path:@"user/register" parameters:dic onComplete:block];
}


- (void)getVerifycodeWithPhoneNum:(NSString *)phoneNum OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = @{
                          @"phone":phoneNum.length > 0 ? phoneNum:@""
                          };
    [self baseReqeustWithMethod:@"POST" path:@"customer/sms" parameters:dic onComplete:block];
}

-(void)loginWithPhoneNum:(NSString *)phoneNum code:(NSString *)code OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = @{
                          @"phone":phoneNum.length > 0 ? phoneNum:@"",
                          @"code":code.length > 0 ? code:@""
                          };
    [self baseReqeustWithMethod:@"POST" path:@"customer/login" parameters:dic onComplete:block];

}

-(void)sendLocation:(NSString *)location OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = @{
                          @"location":location.length > 0 ? location:@""
                          };
    [self baseReqeustWithMethod:@"GET" path:@"customer/position" parameters:dic onComplete:block];
}
-(void)registerWX:(NSString *)openid unionid:(NSString *)unionid OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = @{
                          @"openid":openid.length > 0 ? openid:@"",
                          @"unionid":unionid.length > 0 ? unionid:@"",
                          @"appId":@"wxcbe0e928545754a4",
                          @"channel":@"APP"
                          };
    [self baseReqeustWithMethod:@"POST" path:@"customer/wechatlogin" parameters:dic onComplete:block];
}

- (void)getHomeInfor:(NSString *)position OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    if (position != nil && position.length > 0) {
        dic = @{
                              @"position":position
                              };
    }
      [self baseReqeustWithMethod:@"GET" path:@"home" parameters:dic onComplete:block];
}

-(void)addAddressWithParams:(NSDictionary *)parameters OnComplete:(RSRequestComplete)block{
    
    [self baseReqeustWithMethod:@"POST" path:@"address/add" parameters:parameters onComplete:block];
}

-(void)removeAddressWithId:(NSString *)id OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    if (id != nil && id.length > 0) {
        dic = @{
                @"id":id
                };
    }
    [self baseReqeustWithMethod:@"POST" path:@"address/del" parameters:dic onComplete:block];
}

-(void)editAddressWithParams:(NSDictionary *)parameters OnComplete:(RSRequestComplete)block{
    
    [self baseReqeustWithMethod:@"POST" path:@"address/edit" parameters:parameters onComplete:block];
}

-(void)setDefaultAddress:(NSString *)id  OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    if (id != nil  ) {
        dic = @{
                @"id":id
                };
    }
    [self baseReqeustWithMethod:@"POST" path:@"address/setdefault" parameters:dic onComplete:block];
}

-(void)addressList:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    [self baseReqeustWithMethod:@"POST" path:@"address/list" parameters:dic onComplete:block];
    
}

-(void)getOrderListWithStatus:(NSString *)status page:(NSString *)page size:(NSString *)size OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"status":status,
            @"page":page,
            @"size":size
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/list" parameters:dic onComplete:block];
    
}

-(void)applyDistributorWithName:(NSString *)name phone:(NSString *)phone reason:(NSString *)reason employee:(NSString *)is_employee OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"name":name,
            @"phone":phone,
            @"reason":reason,
            @"is_employee":is_employee
            };
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/resellerapplication" parameters:dic onComplete:block];
}

-(void)getDistributorWithPage:(NSString *)page size:(NSString *)size OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"page":page,
            @"size":size
            };
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/reseller" parameters:dic onComplete:block];

}

-(void)getCollectionGoods:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/favoriteproductlist" parameters:dic onComplete:block];
}

-(void)addCollect:(NSString *)productId OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"productId":productId
            };
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/addfavoriteproduct" parameters:dic onComplete:block];

}

-(void)removeCollect:(NSString *)productId OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"id":productId
            };
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/delfavoriteproduct" parameters:dic onComplete:block];
}


-(void)getCoupons:(NSString *)page size:(NSString *)size status:(NSString*)status OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"page":page,
            @"size":size,
            @"status":status
            };
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/coupon" parameters:dic onComplete:block];
}

-(void)getCouponLists:(NSString *)page size:(NSString *)size totalPrice:(NSString *)totalPrice OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"page":page,
            @"size":size,
            @"totalPrice":totalPrice
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/getcouponlist" parameters:dic onComplete:block];
}


-(void)getDrawalListWithPage:(NSString *)page size:(NSString *)size OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"page":page,
            @"size":size
            };
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/withdrawrecord" parameters:dic onComplete:block];
}

-(void)getDrawalWithAmount:(NSString *)amount OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"amount":amount
            };
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/withdrawrequest" parameters:dic onComplete:block];
}

-(void)getProductCategory:(NSString *)categoryId page:(NSString *)page size:(NSString *)size OnComplete:(RSRequestComplete)block{
    EFUserLocationItem* location = APP_CONTEXT.locationManager.pinLocation;
    NSString* position =  [NSString stringWithFormat:@"%lf,%lf",location.userCoordinate.latitude,location.userCoordinate.longitude];
    
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"categoryId":categoryId,
            @"position":position,
            @"page":page,
            @"size":size
            };
    [self baseReqeustWithMethod:@"GET" path:@"product/category" parameters:dic onComplete:block];
}


-(void)getProductInfoWithId:(NSString *)productId wareHouseId:(NSString*)wareHouseId   OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"productId":productId,
            @"warehouse_id":wareHouseId != nil ? wareHouseId:@""
            };
    [self baseReqeustWithMethod:@"POST" path:@"product/info" parameters:dic onComplete:block];
 
}

//获取购物车 type 0:商城/社区 1：快速结账
-(void)getCartListWithIsLocalDelivery:(BOOL)isLocalDelivery whCode:(NSString *)whCode type:(NSInteger)type OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"isLocalDelivery":@(isLocalDelivery),
            @"whCode":whCode != nil ? whCode:@"",
            @"type":@(type)
            };
    [self baseReqeustWithMethod:@"GET" path:@"shop/cart" parameters:dic onComplete:block];
}

//快速支付
- (void)getFastCheckoutProduct:(NSString *)str OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"scanResult":str
            };
    [self baseReqeustWithMethod:@"GET" path:@"fastcheckout/getproduct" parameters:dic onComplete:block];
    
}

//同步购物车
- (void)syncCartList:(NSArray *)listProduct total:(NSNumber *)total type:(NSNumber *)type token:(NSString *)token isLocalDelivery:(BOOL)isLocalDelivery whCode:(NSString *)whCode OnComplete:(RSRequestComplete)block{
    //product array 变成 json
    NSString *str = [EDJObjectData jsonStringWithObject:listProduct];
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"list":str,
            @"total":total,
            @"type":type,
            @"token":token,
            @"isLocalDelivery":[NSNumber numberWithBool:isLocalDelivery],
            @"whCode":whCode != nil ? whCode:@""
            };
    [self baseReqeustWithMethod:@"POST" path:@"shop/synccart" parameters:dic onComplete:block];
}

//更新购物车 "type":0, 0:商城/社区 1：快速结账,
- (void)updateCartList:(NSArray *)listProduct total:(NSNumber *)total type:(NSNumber *)type  OnComplete:(RSRequestComplete)block{
    //product array 变成 json
    NSString* whCode  = APP_CONTEXT.whCode;
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"list":listProduct,
            @"total":total,
            @"type":type,
            @"isLocalDelivery": [NSNumber numberWithBool: APP_CONTEXT.isLocalDelivery],
            @"whCode":whCode != nil ? whCode:@"",
            @"token":APP_CONTEXT.accountManager.accountInfo.token
            };
    NSString *str = [EDJObjectData jsonStringWithObject:dic];
    NSDictionary *param = @{
                            @"data":str
                            };
    [self baseReqeustWithMethod:@"POST" path:@"shop/updatecart" parameters:param onComplete:block];
}
//增加购物车
- (void)AddCartId:(NSString *)productId qty:(NSNumber *)qty type:(NSNumber *)type  OnComplete:(RSRequestComplete)block{
    NSString* whCode  = APP_CONTEXT.whCode;
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"id":productId,
            @"qty":qty,
            @"type":type,
            @"isLocalDelivery": [NSNumber numberWithBool: APP_CONTEXT.isLocalDelivery],
            @"whCode":whCode != nil ? whCode:@""
            };
   
    [self baseReqeustWithMethod:@"POST" path:@"shop/addtocart" parameters:dic onComplete:block];
}

//清空购物车
- (void)emptyCart:(NSNumber *)type OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"type":type
            };
    [self baseReqeustWithMethod:@"POST" path:@"shop/emptycart" parameters:dic onComplete:block];
}

//移除不可下单的购物车商品
- (void)removeInvalidGood:(NSNumber *)type productIds:(NSString *)ids OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"type":type, 
            @"product_ids":ids
            };
    [self baseReqeustWithMethod:@"POST" path:@"shop/removeinvalidproduct" parameters:dic onComplete:block];
}

//店铺商品id与下单数量组成的字符串。eg. 17,2|18,3
- (void)createOrder:(NSString *)productList addressID:(NSString *)addressId  position:(NSString*)position type:(NSNumber*)type carrier:(NSNumber*)carrier  OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    if (productList != nil) {
        [dic setValue:productList forKey:@"productList"];
    }
    if (APP_CONTEXT.whCode != nil) {
        [dic setValue:APP_CONTEXT.whCode forKey:@"whNo"];
    }
    if (addressId != nil ) {
        [dic setValue:addressId forKey:@"addrId"];
    }
    if (position != nil ) {
        [dic setValue:position forKey:@"position"];
    }
    if(type != nil){
        [dic setValue:type forKey:@"type"];
    }
    if(carrier != nil){
        [dic setValue:carrier forKey:@"carrier"];
    }
    [self baseReqeustWithMethod:@"POST" path:@"order/createorder" parameters:dic onComplete:block];
}

//更改订单地址
-(void)changeOrderAddress:(NSString *)productList   addressId:(NSString*)addressId OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    if (productList.length > 0) {
        [dic setValue:productList forKey:@"product_id"];
    }
    if(addressId != nil){
        [dic setValue:addressId forKey:@"address_id"];
    }
    [self baseReqeustWithMethod:@"POST" path:@"order/changeorderaddr" parameters:dic onComplete:block];

}

//更改优惠券
-(void)changeCoupon:(NSString *)couponid productList:(NSString *)productList addressID:(NSString *)addressId discount:(NSString*)discount type:(NSNumber*)type carrier:(NSNumber*)carrier OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    if (productList.length > 0) {
        [dic setValue:productList forKey:@"productList"];
    }
    if (APP_CONTEXT.whCode != nil) {
        [dic setValue:APP_CONTEXT.whCode forKey:@"whNo"];
    }
    if (addressId != nil ) {
        [dic setValue:addressId forKey:@"addrId"];
    }
    if (couponid != nil ) {
        [dic setValue:couponid forKey:@"couponId"];
    }
    if(discount != nil){
        [dic setValue:discount forKey:@"discount"];
    }
    if(type != nil){
        [dic setValue:type forKey:@"type"];
    }
    if(carrier != nil){
        [dic setValue:carrier forKey:@"carrier"];
    }
    [self baseReqeustWithMethod:@"POST" path:@"order/choosecoupon" parameters:dic onComplete:block];
}

//更改积分
-(void)changePoints:(NSString *)points productList:(NSString *)productList addressID:(NSString *)addressId couponid:(NSString*)couponid discount:(NSString*)discount type:(NSNumber*)type carrier:(NSNumber*)carrier OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    if (productList.length > 0) {
        [dic setValue:productList forKey:@"productList"];
    }
    if (APP_CONTEXT.whCode != nil) {
        [dic setValue:APP_CONTEXT.whCode forKey:@"whNo"];
    }
    if (addressId != nil ) {
        [dic setValue:addressId forKey:@"addrId"];
    }
    if (points != nil ) {
        [dic setValue:points forKey:@"memberPoints"];
    }
    if (couponid != nil ) {
        [dic setValue:couponid forKey:@"couponId"];
    }
    if (discount != nil ) {
        [dic setValue:discount forKey:@"discount"];
    }
    if(type != nil){
        [dic setValue:type forKey:@"type"];
    }
    if(carrier != nil){
        [dic setValue:carrier forKey:@"carrier"];
    }
    [self baseReqeustWithMethod:@"POST" path:@"order/chooseMemberPoints" parameters:dic onComplete:block];
}

//积分抵扣
-(void)getDiscountPoints:(NSString*)totalAmount couponId:(NSString*)couponId postage:(NSString*)postage  discount:(NSString*)discount OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    if (totalAmount != nil) {
        [dic setValue:totalAmount forKey:@"totalAmount"];
    }
    if (couponId != nil ) {
        [dic setValue:couponId forKey:@"couponId"];
    }
    if (postage != nil ) {
        [dic setValue:postage forKey:@"postage"];
    }
    if (discount != nil ) {
        [dic setValue:discount forKey:@"discount"];
    }
    [self baseReqeustWithMethod:@"POST" path:@"card/getmemberpoints" parameters:dic onComplete:block];
}


//下订单接口 服务器从该接口返回客户端吊起微信接口的信息
- (void)placeOrderProductList:(NSString *)productList addressID:(NSString *)addressId paymentMethod:(NSNumber *)payType
                     couponId:(NSString *)couponId points:(NSString *)points userComment:(NSString *)comments type:(NSNumber*)type carrier:(NSNumber*)carrier  OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    if (productList != nil ) {
        [dic setValue:productList forKey:@"productList"];
    }
    [dic setValue:[NSNumber numberWithBool:APP_CONTEXT.isLocalDelivery] forKey:@"isLocalDelivery"];
    [dic setValue:payType forKey:@"paymentMethod"];
    if (couponId != nil) {
        [dic setValue:couponId forKey:@"couponId"];
    }
    if (points != nil) {
        [dic setValue:points forKey:@"points"];
    }
    if (comments != nil) {
        [dic setValue:comments forKey:@"detail"];
    }
    if (addressId != nil  ) {
        [dic setValue:addressId forKey:@"addrId"];
    }
    if(type != nil){
        [dic setValue:type forKey:@"type"];
    }
    if(carrier != nil){
        [dic setValue:carrier forKey:@"carrier"];
    }
    [dic setValue:[OpenUDID value] forKey:@"device_info"];
    [dic setValue:@"" forKey:@"code"];
    
    [self baseReqeustWithMethod:@"POST" path:@"order/placeorder" parameters:dic onComplete:block];

}

//搜索展示页面
- (void)searchRecentOnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    [self baseReqeustWithMethod:@"GET" path:@"search" parameters:dic onComplete:block];
}

//搜索结果列表
- (void)searchResultKey:(NSString *)key page:(NSInteger)page orderByPrice:(NSInteger)orderByPrice orderBySellQty:(NSInteger)orderBySellQty category:(NSInteger)category isSale:(NSInteger)isSale OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:key forKey:@"keyword"];
    [dic setValue:[NSNumber numberWithInteger:page] forKey:@"pageNo"];
    [dic setValue:[NSNumber numberWithInt:100] forKey:@"pageSize"];
    [dic setValue:[NSNumber numberWithInteger:orderByPrice] forKey:@"orderByPrice"];
    [dic setValue:[NSNumber numberWithInteger:orderBySellQty] forKey:@"orderBySellQty"];
    [dic setValue:[NSNumber numberWithInteger:category] forKey:@"category"];
    [dic setValue:[NSNumber numberWithInteger:isSale] forKey:@"isSale"];
    [self baseReqeustWithMethod:@"GET" path:@"search/list" parameters:dic onComplete:block];
}

//获取订单信息
- (void)requestOrderInfo:(NSString *)orderNo OnComplete:(RSRequestComplete)block{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:orderNo forKey:@"orderNo"];
    [self baseReqeustWithMethod:@"POST" path:@"order/info" parameters:dic onComplete:block];
}

//减去购物车中的某个物品
- (void)subCart:(NSString *)productId qty:(NSNumber *)qty type:(NSNumber *)type barcode:(NSString *)barcode OnComplete:(RSRequestComplete)block{
    NSString* whCode  = APP_CONTEXT.whCode;
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"id":productId,
            @"qty":qty,
            @"type":type,
            @"isLocalDelivery": [NSNumber numberWithBool: APP_CONTEXT.isLocalDelivery],
            @"whCode":whCode != nil ? whCode:@"",
            @"barcode":barcode != nil ? barcode:@""
            };
    
    [self baseReqeustWithMethod:@"POST" path:@"shop/decreasecartqty" parameters:dic onComplete:block];
}

//账户
-(void)myAccount:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/index" parameters:dic onComplete:block];
}

//是否已经申请分销
-(void)checkReseller:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    [self baseReqeustWithMethod:@"POST" path:@"myaccount/checkreseller" parameters:dic onComplete:block];
}




//退换货原因
-(void)getReturnReason:(NSString *)status OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"status":status
            };
    
    [self baseReqeustWithMethod:@"GET" path:@"order/getreason" parameters:dic onComplete:block];
}

//选择退换货
-(void)getReturnProducts:(NSString *)orderNo OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"orderNo":orderNo
            };
    
    [self baseReqeustWithMethod:@"POST" path:@"order/getreturnproduct" parameters:dic onComplete:block];
}

//申请退换货
-(void)requestReturn:(NSString *)body OnComplete:(RSRequestComplete)block{
    NSDictionary *param = @{
                            @"data":body
                            };
    [self baseReqeustWithMethod:@"POST" path:@"order/returnrequest" parameters:param onComplete:block];
}

//退换货记录
-(void)getRetrunRecord:(NSString *)orderId OnComplete:(RSRequestComplete)block{
    NSDictionary *param = @{
                            @"orderId":orderId
                            };
    [self baseReqeustWithMethod:@"POST" path:@"order/returnrecord" parameters:param onComplete:block];
}

//活动专题列表
- (void)getActivityList:(NSNumber *)eventId position:(NSString *)position page:(NSNumber *)page size:(NSNumber *)size OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"eventId":eventId,
            @"position":position,
            @"page":page,
            @"size":size
            };
    [self baseReqeustWithMethod:@"GET" path:@"product/event" parameters:dic onComplete:block];
}

//秒杀列表
- (void)getSaleList:(NSNumber *)eventId position:(NSString *)position page:(NSNumber *)page size:(NSNumber *)size OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"eventId":eventId,
            @"position":position,
            @"page":page,
            @"size":size
            };
    [self baseReqeustWithMethod:@"GET" path:@"product/sale" parameters:dic onComplete:block];
}

//退换货申请详情
- (void)getReturnInfor:(NSString *)returnId OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"returnId":returnId
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/returninfo" parameters:dic onComplete:block];
}

//再来一单
- (void)orderAgainWithOrderNO:(NSString *)orderNo OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"order_no" : orderNo
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/orderagain" parameters:dic onComplete:block];
}

//立即支付
- (void)payNowWithOrderNO:(NSString *)orderNo OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"order_no" : orderNo
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/pay" parameters:dic onComplete:block];

}

//拉取分享信息接口
- (void)getShareInfoWithProductId:(NSString *)productId OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"product_id" : productId
            };
    [self baseReqeustWithMethod:@"GET" path:@"share/getshareinfo" parameters:dic onComplete:block];
}

//保存退换货信息
- (void)saveInfoTrackingNo:(NSString *)trackingNo returnId:(NSString *)id carrier:(NSString *)carrier OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"trackingNo":trackingNo,
            @"id":id,
            @"carrier":carrier
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/returnpackageinfo" parameters:dic onComplete:block];
}

//取消退换货
- (void)cancelReturn:(NSString *)returnId OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"returnId":returnId
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/cancelrequest" parameters:dic onComplete:block];
}

//删除订单
-(void)removeOrder:(NSString *)orderId OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"order_id":orderId
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/del" parameters:dic onComplete:block];
}

//取消订单
- (void)cancelOrder:(NSString *)orderId OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"order_id":orderId
            };
    [self baseReqeustWithMethod:@"POST" path:@"order/cancel" parameters:dic onComplete:block];
}


//保存个推信息
- (void)saveGeTuiInfoPhone:(NSString *)phone openid:(NSString *)openid clientId:(NSString *)clientId OnComplete:(RSRequestComplete)block{
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{
            @"phone":phone != nil ? phone : @"" ,
            @"openid":openid != nil ? openid : @"",
            @"clientId":clientId != nil ? clientId : @""
            };
    [self baseReqeustWithMethod:@"POST" path:@"getui/saveclientinfo" parameters:dic onComplete:block];
}

@end
